﻿package morn.core.components
{
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.text.*;
    import morn.core.handlers.*;
    import morn.core.utils.*;

    public class ComboBox extends Component implements ISkinAbleContainer
    {
        protected var _visibleNum:int = 6;
        protected var _button:Button;
        protected var _list:List;
        protected var _isOpen:Boolean;
        protected var _scrollBar:VScrollBar;
        protected var _itemColors:Array;
        protected var _itemSize:int;
        protected var _labels:Array;
        protected var _selectedIndex:int = -1;
        protected var _selectHandler:Handler;
        protected var _openDirection:String = "down";
        protected var _itemHeight:Number;
        protected var _listHeight:Number;
        private var _skinChanged:Boolean = false;
        public static const UP:String = "up";
        public static const DOWN:String = "down";

        public function ComboBox(param1:String = null, param2:String = null)
        {
            this._itemColors = Styles.comboBoxItemColors;
            this._itemSize = Styles.fontSize;
            this._labels = [];
            this.skin = param1;
            this.labels = param2;
            return;
        }// end function

        public function setSkin(param1:Object) : void
        {
            if (!this._skinChanged)
            {
                this.skin = param1 as String;
            }
            return;
        }// end function

        public function set skinChanged(param1:Boolean) : void
        {
            this._skinChanged = param1;
            return;
        }// end function

        override protected function preinitialize() : void
        {
            mouseChildren = true;
            return;
        }// end function

        override protected function createChildren() : void
        {
            var _loc_1:* = new Button();
            this._button = new Button();
            addChild(_loc_1);
            this._list = new List();
            this._list.mouseHandler = new Handler(this.onlistItemMouse);
            this._scrollBar = new VScrollBar();
            this._list.addChild(this._scrollBar);
            return;
        }// end function

        override protected function initialize() : void
        {
            this._button.btnLabel.align = "left";
            this._button.labelMargin = "5";
            this._button.addEventListener(MouseEvent.MOUSE_DOWN, this.onButtonMouseDown);
            this._list.addEventListener(Event.SELECT, this.onListSelect);
            this._scrollBar.name = "scrollBar";
            this._scrollBar.y = 1;
            return;
        }// end function

        private function onButtonMouseDown(event:MouseEvent) : void
        {
            callLater(this.changeOpen);
            return;
        }// end function

        protected function onListSelect(event:Event) : void
        {
            this.selectedIndex = this._list.selectedIndex;
            return;
        }// end function

        public function get skin() : String
        {
            return this._button.skin;
        }// end function

        public function set skin(param1:String) : void
        {
            if (this._button.skin != param1)
            {
                this._button.skin = param1;
                _contentWidth = this._button.width;
                _contentHeight = this._button.height;
                callLater(this.changeList);
            }
            this._skinChanged = true;
            return;
        }// end function

        protected function changeList() : void
        {
            var _loc_1:* = width - 2;
            var _loc_2:* = this._itemColors[2];
            this._itemHeight = ObjectUtils.getTextField(new TextFormat(Styles.fontName, this._itemSize)).height + 3;
            this.list.itemRender = new XML("<Box><Label name=\'label\' width=\'" + _loc_1 + "\' size=\'" + this._itemSize + "\' height=\'" + this._itemHeight + "\' color=\'" + _loc_2 + "\' x=\'1\' /></Box>");
            this.list.repeatY = this._visibleNum;
            this._scrollBar.x = width - this._scrollBar.width - 1;
            this._list.refresh();
            return;
        }// end function

        protected function onlistItemMouse(event:MouseEvent, param2:int) : void
        {
            var _loc_4:Box = null;
            var _loc_5:Label = null;
            var _loc_3:* = event.type;
            if (_loc_3 == MouseEvent.CLICK || _loc_3 == MouseEvent.ROLL_OVER || _loc_3 == MouseEvent.ROLL_OUT)
            {
                _loc_4 = this.list.getCell(param2);
                _loc_5 = _loc_4.getChildByName("label") as Label;
                if (_loc_3 == MouseEvent.ROLL_OVER)
                {
                    _loc_5.background = true;
                    _loc_5.backgroundColor = this._itemColors[0];
                    _loc_5.color = this._itemColors[1];
                }
                else
                {
                    _loc_5.background = false;
                    _loc_5.color = this._itemColors[2];
                }
                if (_loc_3 == MouseEvent.CLICK)
                {
                    this.isOpen = false;
                }
            }
            return;
        }// end function

        protected function changeOpen() : void
        {
            this.isOpen = !this._isOpen;
            return;
        }// end function

        override public function set width(param1:Number) : void
        {
            super.width = param1;
            this._button.width = _width;
            callLater(this.changeList);
            return;
        }// end function

        override public function set height(param1:Number) : void
        {
            super.height = param1;
            this._button.height = _height;
            return;
        }// end function

        public function get labels() : String
        {
            return this._labels.join(",");
        }// end function

        public function get numLabels() : int
        {
            return this._labels.length;
        }// end function

        public function set labels(param1:String) : void
        {
            if (this._labels.length > 0)
            {
                this.selectedIndex = -1;
            }
            if (Boolean(param1))
            {
                this._labels = param1.split(",");
            }
            else
            {
                this._labels.length = 0;
            }
            callLater(this.changeItem);
            return;
        }// end function

        protected function changeItem() : void
        {
            exeCallLater(this.changeList);
            this._listHeight = this._labels.length > 0 ? (Math.min(this._visibleNum, this._labels.length) * this._itemHeight) : (this._itemHeight);
            this._scrollBar.height = this._listHeight - 2;
            var _loc_1:* = this._list.graphics;
            _loc_1.clear();
            _loc_1.lineStyle(1, this._itemColors[3]);
            _loc_1.beginFill(this._itemColors[4]);
            _loc_1.drawRect(0, 0, (width - 1), this._listHeight);
            _loc_1.endFill();
            var _loc_2:Array = [];
            var _loc_3:int = 0;
            var _loc_4:* = this._labels.length;
            while (_loc_3 < _loc_4)
            {
                
                _loc_2.push({label:this._labels[_loc_3]});
                _loc_3++;
            }
            this._list.array = _loc_2;
            return;
        }// end function

        public function get selectedIndex() : int
        {
            return this._selectedIndex;
        }// end function

        public function set selectedIndex(param1:int) : void
        {
            if (this._selectedIndex != param1)
            {
                var _loc_2:* = param1;
                this._selectedIndex = param1;
                this._list.selectedIndex = _loc_2;
                this._button.label = this.selectedLabel;
                if (this._selectHandler != null)
                {
                    this._selectHandler.executeWith([this._selectedIndex]);
                }
                sendEvent(Event.SELECT);
            }
            return;
        }// end function

        public function get selectHandler() : Handler
        {
            return this._selectHandler;
        }// end function

        public function set selectHandler(param1:Handler) : void
        {
            this._selectHandler = param1;
            return;
        }// end function

        public function get selectedLabel() : String
        {
            return this._selectedIndex > -1 && this._selectedIndex < this._labels.length ? (this._labels[this._selectedIndex]) : (null);
        }// end function

        public function set selectedLabel(param1:String) : void
        {
            this.selectedIndex = this._labels.indexOf(param1);
            return;
        }// end function

        public function get visibleNum() : int
        {
            return this._visibleNum;
        }// end function

        public function set visibleNum(param1:int) : void
        {
            this._visibleNum = param1;
            callLater(this.changeList);
            return;
        }// end function

        public function get itemColors() : String
        {
            return String(this._itemColors);
        }// end function

        public function set itemColors(param1:String) : void
        {
            this._itemColors = StringUtils.fillArrayByString(this._itemColors, param1);
            callLater(this.changeList);
            return;
        }// end function

        public function get itemSize() : int
        {
            return this._itemSize;
        }// end function

        public function set itemSize(param1:int) : void
        {
            this._itemSize = param1;
            callLater(this.changeList);
            return;
        }// end function

        public function get isOpen() : Boolean
        {
            return this._isOpen;
        }// end function

        public function set isOpen(param1:Boolean) : void
        {
            var _loc_2:Point = null;
            if (this._isOpen != param1)
            {
                this._isOpen = param1;
                this._button.selected = this._isOpen;
                if (this._isOpen)
                {
                    _loc_2 = localToGlobal(new Point());
                    this._list.setPosition(_loc_2.x, _loc_2.y + (this._openDirection == DOWN ? (height) : (-this._listHeight)));
                    App.stage.addChild(this._list);
                    App.stage.addEventListener(MouseEvent.MOUSE_DOWN, this.removeList);
                    App.stage.addEventListener(Event.REMOVED_FROM_STAGE, this.removeList);
                    this._list.scrollTo(this._selectedIndex + this._visibleNum < this._list.length ? (this._selectedIndex) : (this._list.length - this._visibleNum));
                }
                else
                {
                    this._list.remove();
                    App.stage.removeEventListener(MouseEvent.MOUSE_DOWN, this.removeList);
                    App.stage.removeEventListener(Event.REMOVED_FROM_STAGE, this.removeList);
                }
            }
            return;
        }// end function

        protected function removeList(event:Event) : void
        {
            if (event == null || event.target == this._list.content || !this._button.contains(event.target as DisplayObject) && !this._list.contains(event.target as DisplayObject))
            {
                this.isOpen = false;
            }
            return;
        }// end function

        public function get scrollBarSkin() : String
        {
            return this._scrollBar.skin;
        }// end function

        public function set scrollBarSkin(param1:String) : void
        {
            this._scrollBar.skin = param1;
            return;
        }// end function

        public function get sizeGrid() : String
        {
            return this._button.sizeGrid;
        }// end function

        public function set sizeGrid(param1:String) : void
        {
            this._button.sizeGrid = param1;
            return;
        }// end function

        public function get scrollBar() : VScrollBar
        {
            return this._scrollBar;
        }// end function

        public function get button() : Button
        {
            return this._button;
        }// end function

        public function get list() : List
        {
            return this._list;
        }// end function

        override public function set dataSource(param1:Object) : void
        {
            _dataSource = param1;
            if (param1 is int || param1 is String)
            {
                this.selectedIndex = int(param1);
            }
            else if (param1 is Array)
            {
                this.labels = (param1 as Array).join(",");
            }
            else
            {
                super.dataSource = param1;
            }
            return;
        }// end function

        public function get openDirection() : String
        {
            return this._openDirection;
        }// end function

        public function set openDirection(param1:String) : void
        {
            this._openDirection = param1;
            return;
        }// end function

        public function get labelColors() : String
        {
            return this._button.labelColors;
        }// end function

        public function set labelColors(param1:String) : void
        {
            this._button.labelColors = param1;
            return;
        }// end function

        public function get labelMargin() : String
        {
            return this._button.labelMargin;
        }// end function

        public function set labelMargin(param1:String) : void
        {
            this._button.labelMargin = param1;
            return;
        }// end function

        public function get labelStroke() : String
        {
            return this._button.labelStroke;
        }// end function

        public function set labelStroke(param1:String) : void
        {
            this._button.labelStroke = param1;
            return;
        }// end function

        public function get labelSize() : Object
        {
            return this._button.labelSize;
        }// end function

        public function set labelSize(param1:Object) : void
        {
            this._button.labelSize = param1;
            return;
        }// end function

        public function get labelBold() : Object
        {
            return this._button.labelBold;
        }// end function

        public function set labelBold(param1:Object) : void
        {
            this._button.labelBold = param1;
            return;
        }// end function

    }
}
